package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarMapper;
import com.ruoyi.system.domain.Car;
import com.ruoyi.system.service.ICarService;

/**
 * 车辆Service业务层处理
 *
 * @author 蒼飞
 * @date 2023-11-24
 */
@Service
public class CarServiceImpl implements ICarService
{
    @Autowired
    private CarMapper carMapper;

    /**
     * 查询车辆
     */
    @Override
    public Car selectCarById(Long id)
    {
        return carMapper.selectCarById(id);
    }

    /**
     * 根据车牌快速查询 车辆和项目信息
     */
    @Override
    public List<Car> searchCarInfomation(Car car) {
        List<Car> cars = carMapper.searchCarInfomation(car.getNumber());
        for(Car item:cars){
            String status = item.getStatus();
            if (status == null || status.equals("0")){
                item.setStatus("未排查");
            }else{
                item.setStatus("已排查");
            }
            String isFound = item.getIsFound();
            if (isFound == null || isFound.equals("0")){
                item.setIsFound("未发现");
            }else{
                item.setIsFound("已发现");
            }
        }
        return cars;
    }

    /**
     * 查询车辆列表
     */
    @Override
    public List<Car> selectCarList(Car car) {
        List<Car> cars = carMapper.selectCarList(car);
        for(Car item:cars){
            String status = item.getStatus();
            if (status == null || status.equals("0")){
                item.setStatus("未排查");
            }else{
                item.setStatus("已排查");
            }
            String isFound = item.getIsFound();
            if (isFound == null || isFound.equals("0")){
                item.setIsFound("未发现");
            }else{
                item.setIsFound("已发现");
            }
        }
        return cars;
    }

    /**
     * 新增车辆
     */
    @Override
    public int insertCar(Car car) {
        //首先查询在当前项目下存不存在该车辆
        Car car1 = carMapper.selectCarByIdAndPlate(car.getProjectId(), car.getNumber());
        try{
            if(car1 == null){
                return carMapper.insertCar(car);
            }else{
                return 0;
            }
        }catch (Exception e){
            return -1;
        }
    }

    /**
     * 修改车辆
     */
    @Override
    public int updateCar(Car car)
    {
        return carMapper.updateCar(car);
    }

    /**
     * 批量删除车辆
     */
    @Override
    public int deleteCarByIds(Long[] ids)
    {
        return carMapper.deleteCarByIds(ids);
    }

    /**
     * 删除车辆信息
     */
    @Override
    public int deleteCarById(Long id)
    {
        return carMapper.deleteCarById(id);
    }
}
